﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.SessionState;
using Newtonsoft.Json;
using System.Drawing;
using System.IO;
using Common;
using Model;
using DAL;
using System.Drawing.Imaging;
using System.Dynamic;

namespace UI.tools
{
    /// <summary>
    /// Upload 的摘要说明
    /// </summary>
    public class Upload : IHttpHandler, IRequiresSessionState
    {

        public void ProcessRequest(HttpContext context)
        {
            string handle = context.Request["handle"];
            context.Response.ContentType = "text/plain";
            if (handle == "upload_img") //上传产品图片
            {
                string path = UploadImg();
                context.Response.Write(path);
            }
            if (handle == "upload_file") //上传附件
            {
                string path = UploadFile();
                context.Response.Write(path);
            }
            if (handle == "upload_active") //上传产品图片
            {
                string path = UploadImg();
                context.Response.Write(path);
            }
            if (handle == "upload_imgbywx") //微信上传图片
            {
                string serverid = context.Request["media_id"];
                string path = UploadImgByWeiXin(serverid);
                context.Response.Write(path);
            }

        }
        public static string UploadImg()
        {
            var content = HttpContext.Current;
            string opath = "/upload/" + DateTime.Now.ToString("yyyy-MM-dd") + "/";
            var rootpath = content.Server.MapPath(opath);
            if (!System.IO.Directory.Exists(rootpath))
            {
                Directory.CreateDirectory(rootpath);
            }
            string path = "";
            for (int j = 0; j < content.Request.Files.Count; j++)
            {
                HttpPostedFile uploadFile = content.Request.Files[j];
                if (uploadFile.ContentLength > 0)
                {
                    string ext = Path.GetExtension(uploadFile.FileName);
                    string unique = DateTime.Now.ToString("HHmmssfff");
                    string file = rootpath + unique + ext;
                    string name = file.Substring(0, file.LastIndexOf('.'));
                    uploadFile.SaveAs(file);
                    string imgpath50 = name + "_50" + ext;
                    string imgpath100 = name + "_100" + ext;
                    string imgpath200 = name + "_200" + ext;
                    string imgpath500 = name + "_500" + ext;
                    string imgpath1000 = name + "_1000" + ext;
                    Thumbnail.MakeThumbnailImage(file, imgpath50, 50, 50);
                    Thumbnail.MakeThumbnailImage(file, imgpath100, 100, 100);
                    Thumbnail.MakeThumbnail(file, imgpath200, 200, 200);
                    Thumbnail.MakeThumbnailImage(file, imgpath500, 500, 500);
                    Thumbnail.MakeThumbnailImage(file, imgpath1000, 1000, 1000);

                    path = opath + unique + ext + "*" + uploadFile.FileName;
                }
            }
            return path;
        }
        public static string UploadFile()
        {
            var content = HttpContext.Current;
            string opath = "/upload/" + DateTime.Now.ToString("yyyy-MM-dd") + "/";
            var rootpath = content.Server.MapPath(opath);
            if (!System.IO.Directory.Exists(rootpath))
            {
                Directory.CreateDirectory(rootpath);
            }
            string path = "";
            for (int j = 0; j < content.Request.Files.Count; j++)
            {
                HttpPostedFile uploadFile = content.Request.Files[j];
                if (uploadFile.ContentLength > 0)
                {
                    string ext = Path.GetExtension(uploadFile.FileName);
                    string unique = DateTime.Now.ToString("HHmmssfff");
                    string file = rootpath + unique + ext;
                    string name = file.Substring(0, file.LastIndexOf('.'));
                    uploadFile.SaveAs(file);
                    path = opath + unique + ext + "*" + uploadFile.FileName;
                }
            }
            return path;
        }

        public static string UploadImgByWeiXin(string serverid)
        {
            WeiXin wx = new WeiXin();
            string token = ConfigDAL.GetConfig().Token;
            string path = "/upload/" + DateTime.Now.ToString("yyyy-MM-dd");
            
            string img = path + "/" + DateTime.Now.ToString("yyyyMMddHHmmssffff1") + ".jpg";
            wx.SaveFile("https://file.api.weixin.qq.com/cgi-bin/media/get?access_token=" + token + "&media_id=" + serverid, img);
            
            dynamic dy = new ExpandoObject();
            dy.msg = "上传成功";
            dy.status = 1;
            dy.url = Commons.domain+img;
            Commons.Log(SerializeHelper.ToJsonByDynamic(dy));
            return SerializeHelper.ToJsonByDynamic(dy);
            
        }
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}